'''
Created on 26/06/2010

@author: kivson
'''

class LabelNotFound(Exception):
    pass


class Leitor(object):
    '''
    classdocs
    '''


    def __init__(self,cod):
        '''
        Constructor
        '''
        self.saves = []
        self.cod = cod.splitlines()
        self.pos = -1
        
    def proximo(self, label = None):
        if label:
            pos = self.encontraLabel(label)
            self.pos = pos
        self.pos += 1
        return self.cod[self.pos]
    
    def encontraLabel(self, label):
        for num, cmd in enumerate(self.cod):
            if len (cmd.split()) > 1 and cmd.split()[1] == "NADA":
                if cmd.split()[0] == label:
                    return num -1
        raise LabelNotFound
    
    def retornaLinha(self):
        self.pos = self.saves.pop()
    
    def salvaLinha(self):
        self.saves.append(self.pos)
        
                
                
if __name__ == "__main__":
    cod = '''INPP
    AMEN 3
    AMEN 5
    DSVS L4
    L1 NADA
    IPVL 4
    AMEN 3
    LEIT
    ARMZ 5
    LEIT
    ARMZ 4
    L2 NADA
    '''
    l = Leitor(cod)
    print l.proximo()
    print l.proximo()
    print l.proximo('L1')
    
